package com.learn.controller;

import com.learn.mapper.YmAccreditUseraccreditMapper;
import com.learn.domain.User;
import com.learn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping(value = "/user", method = {RequestMethod.GET, RequestMethod.POST})
public class UserController {
    @Autowired
    private UserService userService;


//    public UserController(UserService userService) {
//        this.userService = userService;
//    }

    //    @Resource
//    private RestTemplate restTemplate;

    @Resource
    private YmAccreditUseraccreditMapper mapper;

    @RequestMapping("/listone")
    @ResponseBody
    public User listOne(String username) {
        return userService.findByUsername(username);
    }


    @RequestMapping("/listall")
    @ResponseBody
    public List<User> listAll() {
        return userService.listUser();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(User user) {
        int result = userService.insertUser(user);
        if (result >= 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(User user) {
        int result = userService.updateUser(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public String delete(int id) {
        int result = userService.delete(id);
        if (result >= 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get(String message) {
        return "hello, " + message;
    }

//    @GetMapping("/test")
//    public String test(){
//        restTemplate.getForObject("http://127.0.0.1:8080/testGet?id=4", String.class);
//        return "success";
//    }
//
//
//    @GetMapping("/testPost")
//    public String testPost(){
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//        Map<String, Object> map = new HashMap<>();
//        map.put("age", 1);
//        map.put("sex", "Spring Boot 101");
//        map.put("name", "A powerful tool for building web apps.");
//
//
//        HttpEntity<Map<String, Object>> request = new HttpEntity<>(map,headers); //组装
//        String response = restTemplate.postForObject("http://localhost:8080/testPost", request,String.class);
//        System.out.println("666"+response);
//        return "success";
//    }


    @GetMapping("/query")
    public Object query(@RequestParam("id") Integer id) {
        List<Integer> totalList = new LinkedList<>();
        List<Integer> list = new LinkedList<>();
        list.add(id);
        while (true) {
            list = mapper.queryByParentId(list);
            totalList.addAll(list);
            if (CollectionUtils.isEmpty(list)) {
                break;
            }
        }
        System.out.println("总条数："+totalList.size());
        return totalList;
    }

    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "Hello world";
    }
}
